<?php

class InstallController extends AppController {

//in order to use file installer based on database.php : no Acl component and app_controller.php


	var $uses = array();
	var $helpers = array('Form');
	var $components = array('RequestHandler');
	var $layout = 'system';
	var $log = null;
	
	function done(){
		$this->pageTitle = 'Installer - Congratz, it\'s done';
		
		//delete installer (install_controller)
		$error = false;
		
		/*if(unlink(__FILE__)) $this->log .= '<p>Installer file has been removed. You can connect to admin area.</p>';
		else{
			$error = true;
			$this->log .= '<p class="error">Error : Installer file has not be removed. You should manually delete app/controllers/install_controller.php</p>';
		}*/
		
		$this->Session->setFlash($this->log);
		//$this->redirect('admin/index');
	}
	
	function index(){
		$this->redirect('/install/step1');
		
	}

	function step1(){
		$error = false;
		$this->pageTitle = 'Installer - Step1 : Database configuration';
		
		//create database.php
		if($this->RequestHandler->isPost()){
			$db_file = "<?php class DATABASE_CONFIG {\n";
			$db_file .= "var \$default = array(\n";
			$db_file .= "	'driver' => 'mysql',\n";
			$db_file .= "	'connect' => 'mysql_connect',\n";
			$db_file .= "	'host' => '{$this->data['Form']['host']}',\n";
			$db_file .= "	'login' => '{$this->data['Form']['login']}',\n";
			$db_file .= "	'password' => '{$this->data['Form']['passwd']}',\n";
			$db_file .= "	'database' => '{$this->data['Form']['db']}',\n";
			$db_file .= "	'prefix' => ''\n";
			$db_file .= ");} ?>";
			if(x_file_write(CONFIGS.'database.php', 'w+', $db_file)) 
				$this->log .= '<p>Database connection file has been successfully writen.</p>';
			else{
				$this->log .= '<p class="error">Error : Check configuration, is app/configs/ writable?</p>';
				$error = true;
			}
			
			if(!$error){
				//check db connection
				uses('model' . DS . 'connection_manager'); 
				$db = ConnectionManager::getInstance();
				$connected = $db->getDataSource('default');
				if(!$connected->isConnected())	{
					$this->log .= '<p class="error">Error : App is not able to connect to database </p>';
					$error = true;
				}else{
					$this->log .= '<p>App can connect to database.</p>';
					
					//insert db schema
					$connected->execute('SET NAMES utf8');
					$file_content = file(CONFIGS.'sql'.DS.'x6tm_cms.blank.sql');
					$query = '';
					foreach($file_content as $sql_line) {
						$tsl = trim($sql_line);
						if (($sql_line != '') && (substr($tsl, 0, 2) != '--') && (substr($tsl, 0, 1) != '#')) {
							$query .= $sql_line;
							if(preg_match('/;\s*$/', $sql_line)) {
								$query = str_replace(';', '', $query);
								$result = $connected->execute($query);
								if (!$result){
									$this->log .= '<p class="error">Error : App is not able to execute request</p>';
									$error = true;
									die($connected->error);
								}
								$query = '';
							}
						}
					}
				}
			}
			
			$this->Session->setFlash($this->log);
			if(!$error){
				$this->redirect('/install/step2');
			}
		}
	}
	
	function step2(){
		$this->pageTitle = 'Installer - Step2 : Register an administrator';
		
		//insert 1st Admin user
		loadModel('User');
		//seems that don't work :
		//loadComponent('Acl');
		
		
		
		$error = false;
		$this->User = new User();
		//$this->Acl = new Acl();
		
		//check if user's table empty
		if($this->User->findCount()){
			$this->Session->setFlash('There\'s already users in database.');
			$this->redirect('/install/done');
		}
		
		if($this->RequestHandler->isPost()){
			if($this->User->save($this->data)){
				//all is done
				$this->log .= '<p>'.$this->data['User']['username'].' has been saved as administrator</p>';
			}else{
				//needed?
				unset($this->data['User']['password']);
				$error = true;
				$this->log .= '<p class="error">Admin user has not been saved.</p>';
			}
			
			$this->Session->setFlash($this->log);
			if(!$error)				
			$this->redirect('install/done');
				
			
		}
		
	}

//eoc
}

?>
